'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const {openId,isNature} = event;
	const db = uniCloud.database();
	const collection = db.collection('users');
	let res = await collection.where({"openId":openId}).get();
	//返回数据给客户端
	console.log("登录:",res.data.length);
	if(res.data&&res.data.length > 0 ){
		console.log("登录:",res.data[0]);
		return res.data[0];
	}else{
		 let timezone = 8; //目标时区时间，东八区   东时区正数 西市区负数
		  let offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差，单位为分钟
		  let nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜（GMT 时间）之间的毫秒数
		  let targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);
		  let nowTime=targetDate.getFullYear() + "-" + parseInt(targetDate.getMonth() + 1)  + "-" + targetDate.getDate()+" "+targetDate.getHours()+":"+targetDate.getMinutes()+":"+targetDate.getSeconds();
		//时间戳为10位需*1000，时间戳为13位的话不需乘1000

		let user = {
			openId:openId,
			isNature:isNature,
			vipLevel:0,
			expireDate:0,
			creatTime:nowTime
		}
	  let addRes = await collection.add(user);
	  console.log("注册addRes:",addRes);
	  let res = await collection.where({"openId":openId}).get();
	  if(res.data&&res.data.length > 0 ){
		console.log("注册:",res);
		return res.data[0];
	  }
	}
	
};
